from fastapi import FastAPI
from redis.asyncio.client import Redis

from src.api.providers.auth import AuthProvider
from src.config import Settings
from src.database.main import build_sa_engine, build_sa_session_factory, create_redis
from src.database.services import UserService
from src.database.uow.infra import InfraProvider, get_user_service

from ...core.api_client.sms import SMSApiClient
from ...core.models.dto import UserDTO
from ...database.uow import SQLAlchemyUoW
from .stub import Stub


def setup_providers(
    app: FastAPI,
    config: Settings,
) -> None:
    engine = build_sa_engine(config.db)

    session_factory = build_sa_session_factory(engine)

    auth_provider = AuthProvider(config.auth)
    redis = create_redis(config.redis)
    provider = InfraProvider(session_factory)
    sms_api_client = SMSApiClient(config.sms_api.login, config.sms_api.password)

    app.dependency_overrides[Stub(UserDTO)] = auth_provider.get_current_user
    app.dependency_overrides[Stub(SQLAlchemyUoW)] = provider.get_uow
    app.dependency_overrides[Stub(UserService)] = get_user_service
    app.dependency_overrides[Stub(Settings)] = lambda: config
    app.dependency_overrides[Stub(AuthProvider)] = lambda: auth_provider
    app.dependency_overrides[Stub(Redis)] = lambda: redis
    app.dependency_overrides[Stub(SMSApiClient)] = lambda: sms_api_client
